package com.base.mine.presenter;

import android.app.Activity;


import com.base.commonlib.base.BaseApp;
import com.base.commonlib.base.presenter.BasePresenter;
import com.base.commonlib.bean.Partner;
import com.base.commonlib.bean.PartnerDao;
import com.base.commonlib.http.RealNetWorkUtil;
import com.base.commonlib.http.utils.CallBackImpl;
import com.base.mine.view.PartnerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import io.reactivex.disposables.Disposable;

/**
 * Created by weikailiang on 2019/6/11.
 */

public class PartnerPresenter extends BasePresenter<PartnerView> {
    public List<Partner> mPartners = new ArrayList<>();
    public int mCurrentIndex=-1;

    public PartnerPresenter(PartnerView view, Activity activity) {
        super(view, activity);
    }

    @Override
    public void start() {
        super.start();
        getPartners();
    }
    private void getPartners() {
        mView.showPb();
        Disposable enqueue = RealNetWorkUtil.postContent().method("api-basic/user/getPartner").content(new HashMap<String, String>())
                .build().enqueueForArray(Partner.class, new CallBackImpl<List<Partner>>() {
                    @Override
                    public void onError(int code, String msg, String errMsg) {
                        mView.hidePb();
                        mView.showError();
                    }

                    @Override
                    public void onNext(List<Partner> partners) {
                        mView.hidePb();
                        if (partners != null) {
                            mPartners.addAll(partners);
                            Partner partner = new Partner();
                            partner.setId(mUser.getUserId());
                            mPartners.remove(partner);
                            mCurrentIndex = mPartners.indexOf(mCurrentPartner);
                            if (mCurrentIndex != -1) {
                                mView.setCurrentPartner(mCurrentPartner.getUserName());
                            }

                            mView.showPartner();
                        }
                    }
                });

        mCompositeDisposable.add(enqueue);


    }
    public void setPartner() {
        if(mCurrentIndex!=-1)
        {
            Partner partner = mPartners.get(mCurrentIndex);
            BaseApp.setPatter(partner,mUser);
            mCurrentPartner = partner;
        }
    }

}
